<template>
	<view>
		<view class="settle-container">
			<label class="radio" @click="changeAllState">
				<radio :checked="checkAll" color="#c00000" value="" />
				<text style="vertical-align: middle;">全选</text>
			</label>
			<view class="totalprice">
				<text>合计：</text>
				<text class="total">￥{{ totalPrice.toFixed(2) }}</text>
			</view>
			<view class="order-btn" @click="order">结算（{{ total }}）</view>
		</view>
	</view>
</template>

<script>
import { mapGetters, mapState } from 'vuex'
export default {
	name: 'my-settle',
	computed: {
		...mapGetters('m_cart', ['total', 'totalPrice', 'checkAll']),
		...mapGetters('m_user', ['addStr']),
		...mapState('m_user', ['token']),
		...mapState('m_cart', ['cartList'])
	},
	data() {
		return {
			seconds: 3
		}
	},
	methods: {
		changeAllState() {
			this.$store.commit('m_cart/setGoodsCheckedAll', !this.checkAll)
		},
		order() {
			if (this.total == 0) uni.$showToast('请先选择要结算的商品')
			if (!this.addStr) uni.$showToast('请先选择收货地址')
			if (!this.token) {
				uni.showToast({
					title: `暂未登录，${this.seconds}秒后跳转到登录页`,
					duration: 1500,
					icon: 'none',
					mask: true
				})
				const timer = setInterval(() => {
					this.seconds--
					if (this.seconds == 0) {
						clearInterval(timer)
						this.$store.commit('m_user/setBackTo', {
							openType: 'switchTab',
							from: '/pages/cart/cart'
						})
						uni.switchTab({ url: '/pages/my/my' })
						this.seconds = 3
						return
					}
					uni.showToast({
						title: `暂未登录，${this.seconds}秒后跳转到登录页`,
						duration: 1500,
						icon: 'none',
						mask: true
					})
				}, 1000)
				return
			}
			this.payment()
		},
		async payment() {
			const { data: res} = await uni.$http.post('/api/public/v1/my/orders/create', {
				order_price: this.totalPrice,
				consignee_addr: this.addStr,
				goods: this.cartList.filter(v => v.goods_state).map(v => {
					return {
						goods_id: v.goods_id,
						goods_number: v.goods_count,
						goods_price: 0.01
					}
				}),
			})
			if (res.meta.status != 200) this.$showToast('创建订单失败')
			const order_number = res.message.order_number
			const { data: res2} = await uni.$http.post('orders/req_unifiedorder', { order_number })
			if (res2.meta.status != 200) uni.$showToast('订单预支付失败')
			const [err, succ] = await uni.requestPayment(res2.message.pay)
			if (err)  uni.$showToast('支付失败！')
			const { data: res4} = await uni.$http.post('orders/chkOrder', {
				order_number
			})
			if (res4.meta.status != 200) uni.$showToast('查询支付状态失败，请重新刷新')
			uni.showToast({
				title: '支付成功'
			})
		}
	}
}
</script>

<style lang="scss">
.settle-container {
	height: 50px;
	position: fixed;
	border-top: 1rpx solid #dddddd;
	left: 0;
	bottom: 0;
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: space-around;
	background-color: #fff;
	z-index: 10;
	radio {
		transform: scale(0.7);
	}
	.totalprice {
		.total {
			color: #c00000;
		}
	}
	.order-btn {
		width: 200rpx;
		height: 80rpx;
		line-height: 80rpx;
		border-radius: 3px;
		background-color: #c00000;
		color: #fff;
		text-align: center;
	}
}
</style>
